package core

import (
	"flag"
	"fmt"
	"github.com/spf13/viper"
)

var v *viper.Viper

func loadViper() {
	var confFilePath string //配置文件路径
	flag.StringVar(&confFilePath, "c", "./config.yaml", "config file path")
	flag.Parse()
	v = viper.New()
	v.SetConfigFile(confFilePath)
	if err := v.ReadInConfig(); err != nil {
		panic(fmt.Errorf("配置文件加载失败！, err:%s \n", err))
	}
}

func GetViper() *viper.Viper {
	if v == nil {
		loadViper()
	}
	return v
}

// Conf 从文件加载配置
func Conf[T any](key string) (c T, err error) {
	if v == nil {
		loadViper()
	}
	err = v.UnmarshalKey(key, &c)
	return
}
